package com.zhilei.canal;

import com.zhilei.base.BaseInfoProperties;
import com.zhilei.pojo.DataDictionary;
import com.zhilei.pojo.co.DataDictionaryCO;
import com.zhilei.untils.GsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * canal 同步组件配置类
 *
 * @author 志磊
 * @since 2024-09-27
 */
@Component
@CanalTable("data_dictionary")
public class DataDictSyncHelper extends BaseInfoProperties implements EntryHandler<DataDictionaryCO> {

    public static final String DD_KEY = DATA_DICTIONARY_LIST_TYPECODE + ":";

    /**
     * 数据库新增时进行缓存同步
     *
     * @param dataDictionaryCO
     */
    @Override
    public void insert(DataDictionaryCO dataDictionaryCO) {
        // TODO 在redis中去获取key
        String ddKey = DD_KEY + dataDictionaryCO.getType_code();
        String ddPrefix = redis.get(ddKey);
        // TODO 初始化list列表
        List<DataDictionary> dataDictionarieList = null;
        // TODO 校验redis中是否存在，如果不存在就设置一个空list,存在就在存在的基础上进行更新
        if (StringUtils.isNotBlank(ddPrefix)) {
            // TODO 如果存在就将存在的数据转换成list
            dataDictionarieList = GsonUtils.stringToListAnother(ddPrefix, DataDictionary.class);
        } else {
            // TODO 不存在就直接进行初始化
            dataDictionarieList = new ArrayList<>();
        }

        // TODO 将同步的表数据转换成java的实体类进行映射
        DataDictionary dataDictionary = constrDD(dataDictionaryCO);
        // TODO 加入到list中
        dataDictionarieList.add(dataDictionary);
        // TODO 再次将list转换成json字符串并设置到redis中
        redis.set(ddKey, GsonUtils.object2String(dataDictionarieList));
    }

    /**
     * 将表字段映射转换成java的实体类
     *
     * @param dataDictionaryCO
     * @return
     */
    private DataDictionary constrDD(DataDictionaryCO dataDictionaryCO) {
        DataDictionary dataDictionary = new DataDictionary();
        BeanUtils.copyProperties(dataDictionaryCO, dataDictionary);
        dataDictionary.setItemKey(dataDictionaryCO.getItem_key());
        dataDictionary.setItemValue(dataDictionaryCO.getItem_value());
        dataDictionary.setTypeCode(dataDictionaryCO.getType_code());
        dataDictionary.setTypeName(dataDictionaryCO.getType_name());
        return dataDictionary;
    }

    /**
     * 修改前修改后
     *
     * @param before
     * @param after
     */
    @Override
    public void update(DataDictionaryCO before, DataDictionaryCO after) {
        // TODO 在redis中去获取key
        String ddKey = DD_KEY + after.getType_code();
        String ddPrefix = redis.get(ddKey);
        // TODO 初始化list列表
        List<DataDictionary> dataDictionarieList = null;
        if (StringUtils.isNotBlank(ddPrefix)) {
            // TODO 如果存在就将存在的数据转换成list
            dataDictionarieList = GsonUtils.stringToListAnother(ddPrefix, DataDictionary.class);

            for (DataDictionary dd : dataDictionarieList) {
                // TODO 将同步的表数据转换成java的实体类进行映射
                DataDictionary dataDictionary = constrDD(after);
                // TODO 找到需要修改的数据并进行修改
                if (dd.getId().equalsIgnoreCase(after.getId())) {
                    dataDictionarieList.remove(dd);
                    dataDictionarieList.add(dataDictionary);
                    break;
                }
            }
        }
        // TODO 再次将list转换成json字符串并设置到redis中
        redis.set(ddKey, GsonUtils.object2String(dataDictionarieList));
    }

    /**
     * 删除
     *
     * @param dataDictionaryCO
     */
    @Override
    public void delete(DataDictionaryCO dataDictionaryCO) {
        // TODO 在redis中去获取key
        String ddKey = DD_KEY + dataDictionaryCO.getType_code();
        String ddPrefix = redis.get(ddKey);
        // TODO 初始化list列表
        List<DataDictionary> dataDictionarieList = null;
        // TODO 校验redis中是否存在，如果不存在就设置一个空list,存在就在存在的基础上进行更新
        if (StringUtils.isNotBlank(ddPrefix)) {
            // TODO 如果存在就将存在的数据转换成list
            dataDictionarieList = GsonUtils.stringToListAnother(ddPrefix, DataDictionary.class);

            for (DataDictionary dataDictionary : dataDictionarieList) {
                if (dataDictionary.getId().equalsIgnoreCase(dataDictionaryCO.getId())) {
                    dataDictionarieList.remove(dataDictionary);
                    break;
                }
            }
        }

        // TODO 再次将list转换成json字符串并设置到redis中
        redis.set(ddKey, GsonUtils.object2String(dataDictionarieList));
    }
}
